<?php
session_start();
require_once '../config.php';

// 检查是否登录
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}


// 获取系统设置
$settings = getSettings();

// 获取管理员信息
$stmt = $pdo->prepare("SELECT * FROM admins WHERE id = ?");
$stmt->execute([$_SESSION['admin_id']]);
$admin = $stmt->fetch();

$success_msg = '';
$error_msg = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $new_password = $_POST['new_password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';

    // 验证数据
    if (empty($username)) {
        $error_msg = '用户名不能为空';
    } elseif (!empty($new_password)) {
        // 只验证两次新密码是否一致
        if ($new_password !== $confirm_password) {
            $error_msg = '两次输入的新密码不一致';
        }
    }

    // 如果没有错误，执行更新
    if (empty($error_msg)) {
        try {
            // 准备更新数据
            $params = ['username' => $username];
            $sql = "UPDATE admins SET username = :username";
            
            // 如果提供了新密码，则更新密码
            if (!empty($new_password)) {
                $params['password'] = password_hash($new_password, PASSWORD_DEFAULT);
                $sql .= ", password = :password";
            }
            
            $sql .= " WHERE id = :id";
            $params['id'] = $_SESSION['admin_id'];
            
            // 执行更新
            $stmt = $pdo->prepare($sql);
            $stmt->execute($params);
            
            // 更新会话中的用户名
            $_SESSION['admin_username'] = $username;
            $admin['username'] = $username;
            
            $success_msg = '个人信息修改成功';
        } catch (PDOException $e) {
            $error_msg = '修改失败：' . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title><?php echo htmlspecialchars($settings['title']); ?> - 个人信息</title>
    <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link rel="stylesheet" type="text/css" href="../assets/css/materialdesignicons.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/animate.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/style.min.css">
</head>

<body>
<!--页面loading-->
<div id="lyear-preloader" class="loading">
    <div class="ctn-preloader">
        <div class="round_spinner">
            <div class="spinner"></div>
            <img src="../assets/images/loading-logo.png" alt="">
        </div>
    </div>
</div>
<!--页面loading end-->

<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <?php $current_page = 'profile'; include 'navbar.php'; ?>
        
        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid p-t-15">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <h4>修改个人信息</h4>
                            </div>
                            <div class="card-body">
                                <?php if (!empty($success_msg)): ?>
                                <div class="alert alert-success alert-dismissible fade show" role="alert">
                                    <strong>成功！</strong> <?php echo $success_msg; ?>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                                <?php endif; ?>
                                
                                <?php if (!empty($error_msg)): ?>
                                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                    <strong>错误！</strong> <?php echo $error_msg; ?>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                                <?php endif; ?>
                                
                                <form method="post" action="" class="row g-3">
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="username" class="form-label">用户名</label>
                                            <input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($admin['username']); ?>" required>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="new_password" class="form-label">新密码</label>
                                            <input type="password" class="form-control" id="new_password" name="new_password" placeholder="如需修改密码，请输入新密码">
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="confirm_password" class="form-label">确认新密码</label>
                                            <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="请再次输入新密码">
                                        </div>
                                    </div>
                                    
                                    <div class="col-12">
                                        <button type="submit" class="btn btn-primary">保存修改</button>
                                        <button type="reset" class="btn btn-default">重置</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<script type="text/javascript" src="../assets/js/jquery.min.js"></script>
<script type="text/javascript" src="../assets/js/popper.min.js"></script>
<script type="text/javascript" src="../assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../assets/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../assets/js/jquery.mCustomScrollbar.concat.min.js"></script>
<script type="text/javascript" src="../assets/js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="../assets/js/main.min.js"></script>
</body>
</html> 